<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表渲染-key的原理</title>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="vue.js"></script>
</head>
<body>
    <div id="root">
        <h2>人员列表</h2>
        <button @click="add">添加一个人</button>
        <ul>
            <!-- <li v-for="(p,index) in persons" :key="index">
                {{p.name}}-{{p.age}}
                <input type="text">
            </li> -->
            <li v-for="(p,index) in persons" :key="p.id">
                {{p.name}}-{{p.age}}
                <input type="text">
            </li>
        </ul>
       
    </div>
   <script type="text/javascript">
    new Vue({
        el:'#root',
        data: {
            persons:[
                {id:1, name:'张三',age:18},
                {id:2, name:'李四',age:19},
                {id:3, name:'王五',age:20}
            ]
        },
        methods: {
            add() {
                const p = {id:4,name:'老刘',age:40}
                this.persons.unshift(p)
            }
        },
    })
   </script>
</body>
</html>